<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Article extends CI_Model {
    public function __construct() {
    	parent::__construct();
    }
	    
    public function getArticles($options = array()) {   
    	$options = _default(array('sortDirection' => 'asc'), $options);
    	$queryFields = array('art_author', 'art_body', 'art_date', 'art_description', 'art_id', 'art_resources', 'art_title', 'art_page');

    	foreach ($queryFields as $field) {
    		if (isset($options[$field]))
    			$this->db->where($field, $options[$field]);
    	}
    	
    	if (isset($options['limit']) && isset($options['offset']))
    		$this->db->limit($options['limit]'], $options['offset']);
    	else if (isset($options['limit']))
    		$this->db->limit($options['limit]']);
    	
    	if (isset($options['sortBy']))
    		$this->db->order_by($options['sortBy'], $options['sortDirection']);
    	$query = $this->db->get('acs_articles');
    	if ($query->num_rows() == 0)
    		return FALSE;
    	if (isset($options['art_id']))
    		return $query->row_array(0);
    	else
    		return $query->result_array();     
    }
    
    public function getLast10($options = array()) {
        $opts = _default(array('sortBy' => 'art_date', 'sortDirection' => 'desc', 'limit' => 10), $options);
        return $this->getArticles($opts);
    }
    
    public function getArticlesWithLimits($from, $count, $options = array()) {
        $opts = _default(array('limit' => $count, 'offset' => $from), $options);
        return $this->getArticles($opts);
    }
    
    public function updateArticle($options = array()) {
    }
    
    public function insertArticle($options = array()) {
    }
    
    public function deleteArticle($options = array()) {
    	
    }
}
